package com.wn.www.carbeauty.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wn.www.carbeauty.entity.Services;
import com.wn.www.carbeauty.mapper.ServicesMapper;
import com.wn.www.carbeauty.service.IServicesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chan
 * @since 2025-04-11
 */
@Service
public class ServicesServiceImpl extends ServiceImpl<ServicesMapper, Services> implements IServicesService {

    @Autowired
    private ServicesMapper servicesMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Services> servicesList() {
        ListOperations redis = redisTemplate.opsForList();
        String key = "car:beauty:list";
        List<Services> list = redis.range(key, 0, -1);
        if (list == null || list.isEmpty()){
            list = servicesMapper.selectList(new QueryWrapper<>());
            redis.leftPushAll(key,list);
        }
        return list;
    }

    @Override
    public Services servicesById(Integer id) {
        ValueOperations redis = redisTemplate.opsForValue();
        Services services = (Services) redis.get("car:beauty:" + id);
        if (services == null){
            services = servicesMapper.selectById(id);
            redis.set("car:beauty:" + id,services);
        }
        return services;
    }
}
